package lcof;

import com.sun.org.apache.xpath.internal.SourceTree;

/**
 * @author 江岸
 * @version V1.0
 * @ClassName: ReplaceSpace5
 * @description: 请实现一个函数，把字符串 s 中的每个空格替换成"%20"。
 *
 *
 *
 * 示例 1：
 *
 * 输入：s = "We are happy."
 * 输出："We%20are%20happy."
 *
 * 来源：力扣（LeetCode）
 * 链接：https://leetcode-cn.com/problems/ti-huan-kong-ge-lcof
 * 著作权归领扣网络所有。商业转载请联系官方授权，非商业转载请注明出处。
 * @date 2021/2/2410:31
 * @since V1.0
 */
public class ReplaceSpace5 {
    public static void main(String[] args) {
        String str = "We are happy";
        String s = str.replaceAll(" ", "%20");
        //System.out.println(s);
        String st = replaceSpace("he lel o");
        System.out.println(st);
    }

    public static String  replaceSpace(String s){
        char[] arr = new char[s.length()*3];
        int index = 0;
        char c;
        for (int i=0;i<s.length();i++){
            c=s.charAt(i);
            if (' ' == c){
                arr[index]='%';
                arr[++index]='2';
                arr[++index]='0';
            }else {
                arr[index]=c;
            }
            index++;
        }
        String s1 = new String(arr, 0, index);
        return s1;
    }

}
